package StackDemo;

import java.util.Arrays;
import java.util.Stack;

// 最小栈
class MinStack {
    public Stack<Integer> stack;
    public Stack<Integer> minStack;

    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if(minStack.empty()){
            minStack.push(val);
        }else{
            if(val <= minStack.peek()){
                minStack.push(val);
            }
        }

    }

    public void pop() {
        int val = stack.pop();
        if(val == minStack.peek()){
            minStack.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }

}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */



//
//public class Solution {
//    /**
//     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
//     *   栈的压入 弹出序列匹配
//     *
//     * @param pushV int整型一维数组
//     * @param popV int整型一维数组
//     * @return bool布尔型
//     */
//    public boolean IsPopOrder (int[] pushV, int[] popV) {
//        Stack<Integer> stack = new Stack<>();
//        int j = 0;
//        for (int i = 0; i < pushV.length; i++) {
//            stack.push(pushV[i]);
//
//            while(j<popV.length && !stack.empty() && stack.peek() == popV[j]){
//                stack.pop();
//                j++;
//            }
//
//        }
//        if(stack.empty()){
//            return true;
//        }
//        return false;
//    }
//}



public class MyStack {
    public int[] elem;
    public int useSize;

    public MyStack(){
        elem = new int[10];
    }

    public void push(int e){
        if(isFull()){
            elem = Arrays.copyOf(elem, 2*elem.length);
        }
        elem[useSize] = e;
        useSize++;
    }

    private boolean isFull(){
        return useSize == elem.length;
    }

    public int pop(){
        if(isEmpty()){
            return -1;
        }
        useSize--;
        return elem[useSize];
    }

    public int peek(){
        return elem[--useSize];
    }

    public int size(){
        return useSize;
    }

    public boolean isEmpty(){
        return useSize == 0;
    }


}
